package com.ilock.action;

import java.util.HashMap;
import java.util.Map;

import com.ilock.bean.Ilockinfo;
import com.ilock.bean.Iuser;
import com.ilock.service.IUserService;
import com.ilock.service.ILockService;
import com.opensymphony.xwork2.ActionSupport;

public class ShareAction extends ActionSupport {
	
	private IUserService IUserService;
	private ILockService ILockService;
	private Map<String, Object> shareMap;
	private String lockpassword;
	private String useremail;
	private String userpassword;
	
	public IUserService getIUserService() {
		return IUserService;
	}

	public void setIUserService(IUserService iUserService) {
		IUserService = iUserService;
	}

	public ILockService getILockService() {
		return ILockService;
	}

	public void setILockService(ILockService iLockService) {
		ILockService = iLockService;
	}

	public Map<String, Object> getShareMap() {
		return shareMap;
	}

	public void setShareMap(Map<String, Object> shareMap) {
		this.shareMap = shareMap;
	}

	public String getLockpassword() {
		return lockpassword;
	}

	public void setLockpassword(String lockpassword) {
		this.lockpassword = lockpassword;
	}

	public String getUseremail() {
		return useremail;
	}

	public void setUseremail(String useremail) {
		this.useremail = useremail;
	}

	public String getUserpassword() {
		return userpassword;
	}

	public void setUserpassword(String userpassword) {
		this.userpassword = userpassword;
	}

	public String getShareCode()throws Exception{
		Map<String,Object> tempMap = new HashMap<String,Object>();
		if(useremail!=null && userpassword!=null && lockpassword!=null){
			Ilockinfo lockinfo = ILockService.findLockInfoByToken(lockpassword);
			Iuser user = IUserService.checkIn(useremail, userpassword);
			if(user!=null){
				if(lockinfo!=null){
					if(ILockService.checkOwner( user.getIuserNumber(),lockinfo.getIlockInfoNumber())){//确认为锁主人方可绑定
						tempMap.put("shareToken", lockpassword);
						tempMap.put("state", "getsharecode:success");
					}else{
						tempMap.put("state", "getsharecode:owner not found");
					}
				}else{
					tempMap.put("state", "getsharecode:lock not found with wrong token");
				}
			}else{
				tempMap.put("state", "getsharecode:user info error");
			}
		}else{
			tempMap.put("state", "getsharecode:parameter incompleted");
		}
		this.setShareMap(tempMap);
		return "success";
	}

	public String addShare()throws Exception{
		Map<String,Object> tempMap = new HashMap<String,Object>();
		if(useremail!=null && userpassword!=null && lockpassword!=null){
			Ilockinfo lockinfo = ILockService.findLockInfoByToken(lockpassword);
			Iuser user = IUserService.checkIn(useremail, userpassword);
			if(user!=null){
				if(lockinfo!=null){
					if(!ILockService.checkShareExist(user.getIuserNumber(),lockinfo.getIlockInfoNumber())){
						ILockService.addShare(user.getIuserNumber(), lockinfo.getIlockInfoNumber());
						tempMap.put("ilockinfo", lockinfo);
						tempMap.put("shareToken", lockpassword);
						tempMap.put("state", "addShare:success");
					}else{
						tempMap.put("state", "addShare:share not found or already shared");
					}
				}else{
					tempMap.put("state", "addShare:lock not found with wrong token");
				}
			}else{
				tempMap.put("state", "addShare:user info error");
			}
		}else{
			tempMap.put("state", "addShare:parameter incompleted");
		}
		this.setShareMap(tempMap);
		return "success";
	}

}
